﻿var codeMirrorObject = null;
var TemplateSections = {"Footer":0, "Header":1, "LeftNav":2, "TopNav":3, "RightCol":4};
var SourceType = {"Folder":0, "Page":1, "File":2};

function handleSuccessMessages(data) {
    $("#ulValidation").html("<li>" + data + "</li>");
}

function handleErrorMessages(data) {
    $("#ulValidation").html("<li>" + data + "</li>");
}

$(document).ready(function () {
    $('#describemdevelopercontrol').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "Close": function () { $(this).dialog("close"); } } });
    $('#addfiletype').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddFileType($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#editfiletype').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { EditFileType($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addpage').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddPage($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addfolder').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddFolder($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addfile').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "Close": function () { $(this).dialog("close"); } } });
    $('#addmdevelopercontrol').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddMDeveloperControl($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addheader').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddHeader($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addfooter').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddFooter($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addleftnav').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddLeftNav($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addrightcol').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddRightCol($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addtopnav').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddTopNav($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addcss').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddCss($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#addjs').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddJs($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#adduser').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { AddUser($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#edituser').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { EditUser($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletepage').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeletePage($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletefolder').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteFolder($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletefile').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteFile($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletemdevelopercontrol').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteMDeveloperControl($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deleteheader').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteHeader($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletefooter').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteFooter($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deleteleftnav').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteLeftNav($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deleterightcol').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteRightCol($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletetopnav').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteTopNav($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletecss').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteCss($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#deletejs').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { DeleteJs($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renamepage').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenamePage($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renamefolder').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameFolder($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renamefile').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameFile($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renameheader').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameHeader($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renamefooter').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameFooter($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renameleftnav').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameLeftNav($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renamerightcol').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameRightCol($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renametopnav').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameTopNav($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renamecss').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameCss($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#renamejs').dialog({ autoOpen: false, height: 300, width: 400, modal: true, buttons: { "OK": function () { RenameJs($(this)); }, "Close": function () { $(this).dialog("close"); } } });
    $('#permissions').dialog({ autoOpen: false, height: 400, width: 700, modal: true, buttons: { "Close": function () { $(this).dialog("close"); return false; } } });
    LoadDirectory();
    InitializeUploadify();
});

function InitializeUploadify() {
    $('#fileNew').uploadify({
        'auto': false
      , 'buttonText': 'Choose Files'
      , 'fileSizeLimit': '50MB'
      , 'multi': true
      , 'onDialogClose': function () {
          var cookie = $.cookie('Lidocaine');
          var checked = "no";
          if ($('#NewFileUnzipZips').attr('checked')) {
              checked = "yes";
          }
          var path = $('#path').val();
          $('#fileNew').uploadify('settings', 'formData', { 'strFilepath': path, 'strCookie': cookie, 'unzip': checked });
          $('#fileNew').uploadify('upload', '*');
      }
      , 'onUploadSuccess': function (file, data, response) {
          $("#ulValidation").append("<br />" + data);
          LoadDirectory($('#path').val());
      }
      , 'queueSizeLimit': 100
      , 'swf': '/cms/flash/uploadify.swf'
      , 'uploader': '/lidocaine.ashx?f=Upload'
    });
}

function ShowTemplateInterface() {
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 0) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"ShowTemplateInterface();return false;\"><span class=\"icon sprite-settings\"></span>Template Administration</a>");
    $('#explorer').html("<div class=\"schem-wrapper\"><a href=\"#\" onclick=\"LoadHeaders();return false;\" class=\"schem schem-header-top-footer\"><br />HEADER<br /><br /></a><a href=\"#\" onclick=\"LoadCss();return false;\" class=\"schem schem-css-js\"><br />CSS<br /><br /></a><a href=\"#\" onclick=\"LoadJs();return false;\" class=\"schem schem-css-js\"><br />JS<br /><br /></a><a href=\"#\" onclick=\"LoadTopNavs();return false;\" class=\"schem schem-header-top-footer\"><br />TOP NAVIGATION<br /><br /></a><div><a href=\"#\" onclick=\"LoadLeftNavs();return false;\" class=\"schem schem-left-right\"><br /><br />LEFT<br />COLUMN</a><a href=\"#\" onclick=\"LoadMDeveloperControls();return false;\" class=\"schem schem-main\"><br /><br />REGISTER<br />DEVELOPERCONTROLS</a><a href=\"#\" onclick=\"LoadRightCols();return false;\" class=\"schem schem-left-right\"><br /><br />RIGHT<br />COLUMN</a></div><a href=\"#\" onclick=\"LoadFooters();return false;\" class=\"schem schem-header-top-footer\"><br />FOOTER<br /><br /></a></div>");
}

function ShowFileTypeInterface() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    var output = "<a href=\"#\" title=\"Add New File Type\" class=\"button icon sprite-newpage\" onclick=\"$('#addfiletype').dialog('open');return false;\" />";
    $('#ribbon').prepend(output);
    $('.breadcrumbs').children().each(function (index) { if (index > 0) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"ShowFileTypeInterface();return false;\"><span class=\"icon sprite-settings\"></span>Hosted File Types</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=LoadFileTypes"
      , type: "GET"
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          output = '';
          for (var i = 0; i < oJSON.length; i++) {
              output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-delete\" onclick=\"if(confirm('Are you sure you want to delete this file type?')) DeleteFileType('" + oJSON[i].FileTypeID + "'); return false;\" /><a href=\"#\" onclick=\"$('#EditFileTypeExtension').html('" + oJSON[i].Extension + "');$('#pagePath').val('" + oJSON[i].FileTypeID + "');$('#EditFileTypeMime').val('" + oJSON[i].Mime + "');$('#editfiletype').dialog('open');return false;\" title=\"Edit\" class=\"button icon sprite-edit\" /><a href=\"#\" onclick=\"$('#EditFileTypeExtension').html('" + oJSON[i].Extension + "');$('#pagePath').val('" + oJSON[i].FileTypeID + "');$('#EditFileTypeMime').val('" + oJSON[i].Mime + "');$('#editfiletype').dialog('open');return false;\" title=\"Click To Edit\" class=\"afilename\"><span class=\"icon sprite-new\"></span><span class=\"filetype-extension\">" + oJSON[i].Extension + "</span>" + oJSON[i].Mime + "</a></div>";
          }

          $('#explorer').html(output);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function ShowUsersInterface() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    var output = "<a href=\"#\" title=\"Add New User\" class=\"button icon sprite-newuser\" onclick=\"$('#adduser').dialog('open');return false;\" />";
    $('#ribbon').prepend(output);
    $('.breadcrumbs').children().each(function (index) { if (index > 0) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"ShowUsersInterface();return false;\"><span class=\"icon sprite-settings\"></span>Application Users</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=LoadUsers"
      , type: "GET"
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          output = '';
          for (var i = 0; i < oJSON.length; i++) {
              output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-delete\" onclick=\"if(confirm('Are you sure you want to delete this user?')) DeleteUser('" + oJSON[i].UserID + "'); return false;\" /><a href=\"#\" onclick=\"$('#EditUserName').val('" + oJSON[i].UserName + "');" + (oJSON[i].SiteAdministrator == "1" ? "$('#EditUserRoleSiteAdministrator').attr('checked', 'checked');" : "$('#EditUserRoleSiteAdministrator').removeAttr('checked');") + (oJSON[i].ContentManager == "1" ? "$('#EditUserRoleContentManager').attr('checked', 'checked');" : "$('#EditUserRoleContentManager').removeAttr('checked');") + (oJSON[i].ContentProvider == "1" ? "$('#EditUserRoleContentProvider').attr('checked', 'checked');" : "$('#EditUserRoleContentProvider').removeAttr('checked');") + "$('#pagePath').val('" + oJSON[i].UserID + "');$('#edituser').dialog('open');return false;\" title=\"Edit\" class=\"button icon sprite-edit\" /><a href=\"#\" onclick=\"$('#EditUserName').val('" + oJSON[i].UserName + "');" + (oJSON[i].SiteAdministrator == "1" ? "$('#EditUserRoleSiteAdministrator').attr('checked', 'checked');" : "$('#EditUserRoleSiteAdministrator').removeAttr('checked');") + (oJSON[i].ContentManager == "1" ? "$('#EditUserRoleContentManager').attr('checked', 'checked');" : "$('#EditUserRoleContentManager').removeAttr('checked');") + (oJSON[i].ContentProvider == "1" ? "$('#EditUserRoleContentProvider').attr('checked', 'checked');" : "$('#EditUserRoleContentProvider').removeAttr('checked');") + "$('#pagePath').val('" + oJSON[i].UserID + "');$('#edituser').dialog('open');return false;\" title=\"Click To Edit\" class=\"afilename\"><span class=\"icon sprite-user\"></span>" + oJSON[i].UserName + "</a></div>";
          }

          $('#explorer').html(output);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function DeleteUser(userID) {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=DeleteUser"
      , type: "POST"
      , data: { "userID": userID }
      , success: function (data) {
          handleSuccessMessages(data);
          ShowUsersInterface();
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function EditUser(dialog) {
    $('#editUserValidation').html('');
    var uName = $("#EditUserName").val();
    var uPassword = $("#EditUserPassword").val();
    if (uName.length < 1) {
        $('#editUserValidation').append("A username cannot be blank.");
    }

    if (uPassword.length < 1) {
        $('#editUserValidation').append("A password cannot be blank.");
    }

    if ($('#editUserValidation').html() == '') {
        $(".loading").show();
        $.ajax({
            url: "/lidocaine.ashx?f=UpdateUser"
          , type: "POST"
          , data: { "userID": $("#pagePath").val(), "username": uName, "password": uPassword, "SiteAdministrator": $("#EditUserRoleSiteAdministrator").is(":checked") ? 1 : 0, "ContentManager": $("#EditUserRoleContentManager").is(":checked") ? 1 : 0, "ContentProvider": $("#EditUserRoleContentProvider").is(":checked") ? 1 : 0 }
          , success: function (data) {
              dialog.dialog('close');
              $("#EditUserName").html("");
              $("#EditUserPassword").val("");
              $("#EditUserRoleSiteAdministrator").removeAttr("checked");
              $("#EditUserRoleContentManager").removeAttr("checked");
              $("#EditUserRoleContentProvider").removeAttr("checked");
              handleSuccessMessages(data);
              ShowUsersInterface();
              $('.loading').hide();
          }
          , error: function (data) {
              handleErrorMessages(data.responseText);
          }
        });
    }
}

function AddUser(dialog) {
    $('#newUserValidation').html('');
    var uName = $("#NewUserName").val();
    var uPassword = $("#NewUserPassword").val();
    if (uName.length < 1) {
        $('#newUserValidation').append("A new username cannot be blank.");
    }
    
    if (uPassword.length < 1) {
        $('#newUserValidation').append("A new password cannot be blank.");
    }

    if ($('#newUserValidation').html() == '') {
        $(".loading").show();
        $.ajax({
            url: "/lidocaine.ashx?f=AddUser"
          , type: "POST"
          , data: { "username": uName, "password": uPassword, "SiteAdministrator": $("#NewUserRoleSiteAdministrator").is(":checked") ? 1 : 0, "ContentManager": $("#NewUserRoleContentManager").is(":checked") ? 1 : 0, "ContentProvider": $("#NewUserRoleContentProvider").is(":checked") ? 1 : 0 }
          , success: function (data) {
              dialog.dialog('close');
              $("#NewUserName").val("");
              $("#NewUserPassword").val("");
              $("#NewUserRoleSiteAdministrator").removeAttr("checked");
              $("#NewUserRoleContentManager").removeAttr("checked");
              $("#NewUserRoleContentProvider").removeAttr("checked");
              handleSuccessMessages(data);
              ShowUsersInterface();
              $('.loading').hide();
          }
          , error: function (data) {
              handleErrorMessages(data.responseText);
          }
        });
    }
}

function DeleteFileType(filetypeID) {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=DeleteFileType"
      , type: "POST"
      , data: { "filetypeID": filetypeID }
      , success: function (data) {
          handleSuccessMessages(data);
          ShowFileTypeInterface();
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function EditFileType(dialog) {
    $('#editFileTypeValidation').html('');
    var ftMime = $("#EditFileTypeMime").val();
    if (ftMime.length < 1) {
        $('#editFileTypeValidation').append("A file type MIME encoding cannot be blank.");
    }
    else {
        $(".loading").show();
        $.ajax({
            url: "/lidocaine.ashx?f=UpdateFileType"
          , type: "POST"
          , data: { "filetypeID": $("#pagePath").val(), "mime": ftMime }
          , success: function (data) {
              dialog.dialog('close');
              $("#EditFileTypeExtension").html("");
              $("#EditFileTypeMime").val("");
              handleSuccessMessages(data);
              ShowFileTypeInterface();
              $('.loading').hide();
          }
          , error: function (data) {
              handleErrorMessages(data.responseText);
          }
        });
    }
}

function AddFileType(dialog) {
    $('#newFileTypeValidation').html('');
    var ftExtension = $("#NewFileTypeExtension").val();
    var ftMime = $("#NewFileTypeMime").val();
    if (ftExtension.length < 1) {
        $('#newFileTypeValidation').append("A new file type extension cannot be blank.");
    }
    if (ftMime.length < 1) {
        $('#newFileTypeValidation').append("A new file type MIME encoding cannot be blank.");
    }
    else if (!ftExtension.match(/^[\w]+$/)) {
        $('#newFileTypeValidation').append("Please use only alphanumeric characters for the file type extension.");
    }
    
    if ($('#newFileTypeValidation').html() == '') {
        $(".loading").show();
        $.ajax({
            url: "/lidocaine.ashx?f=AddFileType"
          , type: "POST"
          , data: { "extension": ftExtension, "mime": ftMime }
          , success: function (data) {
              dialog.dialog('close');
              $("#NewFileTypeExtension").val("");
              $("#NewFileTypeMime").val("");
              handleSuccessMessages(data);
              ShowFileTypeInterface();
              $('.loading').hide();
          }
          , error: function (data) {
              handleErrorMessages(data.responseText);
          }
        });
    }
}

function UpdatePageSettingsTitle(pageID, pageTitle) {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=UpdatePageSettingsTitle"
      , type: "POST"
      , data: { "title": pageTitle, "pageID": pageID }
      , success: function (data) {
          handleSuccessMessages(data);
          EditPageSettings($('#pagePath').val());
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function UpdatePageSettingsHeader(pageID, headerID) {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=UpdatePageSettingsHeader"
      , type: "POST"
      , data: { "headerID": headerID, "pageID": pageID }
      , success: function (data) {
          handleSuccessMessages(data);
          EditPageSettings($('#pagePath').val());
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function UpdatePageSettingsFooter(pageID, footerID) {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=UpdatePageSettingsFooter"
      , type: "POST"
      , data: { "footerID": footerID, "pageID": pageID }
      , success: function (data) {
          handleSuccessMessages(data);
          EditPageSettings($('#pagePath').val());
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function UpdatePageSettingsLeftNav(pageID, leftnavID) {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=UpdatePageSettingsLeftNav"
      , type: "POST"
      , data: { "leftnavID": leftnavID, "pageID": pageID }
      , success: function (data) {
          handleSuccessMessages(data);
          EditPageSettings($('#pagePath').val());
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function UpdatePageSettingsRightCol(pageID, leftnavID) {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=UpdatePageSettingsRightCol"
      , type: "POST"
      , data: { "rightcolID": leftnavID, "pageID": pageID }
      , success: function (data) {
          handleSuccessMessages(data);
          EditPageSettings($('#pagePath').val());
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function UpdatePageSettingsTopNav(pageID, topnavID) {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=UpdatePageSettingsTopNav"
      , type: "POST"
      , data: { "topnavID": topnavID, "pageID": pageID }
      , success: function (data) {
          handleSuccessMessages(data);
          EditPageSettings($('#pagePath').val());
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function UpdatePageSettingsCSS(pageID, cssID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsCSS",
        data: { "cssID": cssID, "pageID": pageID },
        success: function (data) {
            handleSuccessMessages(data);
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function UpdatePageSettingsJS(pageID, jsID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsJS",
        data: { "jsID": jsID, "pageID": pageID },
        success: function (data) {
            handleSuccessMessages(data);
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function ChangePageVersion(pageID, contentID) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=UpdatePageSettingsContentVersion",
        data: { "pageID": pageID, "blockID": contentID },
        success: function (data) {
            handleSuccessMessages(data);
            EditPageSettings($('#pagePath').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function EditPageSettings(pagePath) {
    $('.loading').show();
    $('#pagePath').val(pagePath);
    $('#ribbon').children('a').remove();
    //$('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    //$('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditTopNav('" + navName + "');return false;\"><span class=\"icon sprite-folder\"></span>" + navName + "</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=EditPageSettings"
      , type: "POST"
      , data: { "pagePath": pagePath }
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var orderOptions = '';
          var output = '';

          output += "<div class=\"page-settings-field\"><label for=\"pageSettingsFieldTitle\">Title:</label><input id=\"pageSettingsFieldTitle\" type=\"text\" value=\"" + oJSON[0].pTitle + "\" maxlength=\"500\" onchange=\"UpdatePageSettingsTitle('" + oJSON[0].pID + "', $(this).val()); return false;\" /></div>";

          orderOptions = "<option value=\"\"></option>";
          for (var q = 0; q < oJSON[0].Headers.length; q++) {
              if (oJSON[0].Headers[q].ID == oJSON[0].pHead) {
                  orderOptions += "<option selected=\"selected\" value=\"" + oJSON[0].Headers[q].ID + "\">" + oJSON[0].Headers[q].Name + "</option>";
              }
              else {
                  orderOptions += "<option value=\"" + oJSON[0].Headers[q].ID + "\">" + oJSON[0].Headers[q].Name + "</option>";
              }
          }
          output += "<div class=\"page-settings-field\"><label for=\"pageSettingsFieldHeader\">Header:</label><select id=\"pageSettingsFieldHeader\" onchange=\"UpdatePageSettingsHeader('" + oJSON[0].pID + "', $(this).val()); return false;\">" + orderOptions + "</select></div>";

          orderOptions = "<option value=\"\"></option>";
          for (var s = 0; s < oJSON[0].CSSs.length; s++) {
              if (oJSON[0].CSSs[s].ID == oJSON[0].pCss) {
                  orderOptions += "<option selected=\"selected\" value=\"" + oJSON[0].CSSs[s].ID + "\">" + oJSON[0].CSSs[s].Name + "</option>";
              }
              else {
                  orderOptions += "<option value=\"" + oJSON[0].CSSs[s].ID + "\">" + oJSON[0].CSSs[s].Name + "</option>";
              }
          }
          output += "<div class=\"page-settings-field\"><label for=\"pageSettingsFieldCSS\">Cascading Style Sheet:</label><select id=\"pageSettingsFieldCSS\" onchange=\"UpdatePageSettingsCSS('" + oJSON[0].pID + "', $(this).val()); return false;\">" + orderOptions + "</select></div>";

          orderOptions = "<option value=\"\"></option>";
          for (var t = 0; t < oJSON[0].JSs.length; t++) {
              if (oJSON[0].JSs[t].ID == oJSON[0].pJs) {
                  orderOptions += "<option selected=\"selected\" value=\"" + oJSON[0].JSs[t].ID + "\">" + oJSON[0].JSs[t].Name + "</option>";
              }
              else {
                  orderOptions += "<option value=\"" + oJSON[0].JSs[t].ID + "\">" + oJSON[0].JSs[t].Name + "</option>";
              }
          }
          output += "<div class=\"page-settings-field\"><label for=\"pageSettingsFieldJS\">Javascript:</label><select id=\"pageSettingsFieldJS\" onchange=\"UpdatePageSettingsJS('" + oJSON[0].pID + "', $(this).val()); return false;\">" + orderOptions + "</select></div>";

          orderOptions = "<option value=\"\"></option>";
          for (var q = 0; q < oJSON[0].TopNavs.length; q++) {
              if (oJSON[0].TopNavs[q].ID == oJSON[0].pTopNav) {
                  orderOptions += "<option selected=\"selected\" value=\"" + oJSON[0].TopNavs[q].ID + "\">" + oJSON[0].TopNavs[q].Name + "</option>";
              }
              else {
                  orderOptions += "<option value=\"" + oJSON[0].TopNavs[q].ID + "\">" + oJSON[0].TopNavs[q].Name + "</option>";
              }
          }
          output += "<div class=\"page-settings-field\"><label for=\"pageSettingsFieldTopNav\">Top Navigation:</label><select id=\"pageSettingsFieldTopNav\" onchange=\"UpdatePageSettingsTopNav('" + oJSON[0].pID + "', $(this).val()); return false;\">" + orderOptions + "</select></div>";

          orderOptions = "<option value=\"\"></option>";
          for (var q = 0; q < oJSON[0].LeftNavs.length; q++) {
              if (oJSON[0].LeftNavs[q].ID == oJSON[0].pLeftNav) {
                  orderOptions += "<option selected=\"selected\" value=\"" + oJSON[0].LeftNavs[q].ID + "\">" + oJSON[0].LeftNavs[q].Name + "</option>";
              }
              else {
                  orderOptions += "<option value=\"" + oJSON[0].LeftNavs[q].ID + "\">" + oJSON[0].LeftNavs[q].Name + "</option>";
              }
          }
          output += "<div class=\"page-settings-field\"><label for=\"pageSettingsFieldLeftNav\">Left Column:</label><select id=\"pageSettingsFieldLeftNav\" onchange=\"UpdatePageSettingsLeftNav('" + oJSON[0].pID + "', $(this).val()); return false;\">" + orderOptions + "</select></div>";

          orderOptions = "<option value=\"\"></option>";
          for (var q = 0; q < oJSON[0].RightCols.length; q++) {
              if (oJSON[0].RightCols[q].ID == oJSON[0].pRightCol) {
                  orderOptions += "<option selected=\"selected\" value=\"" + oJSON[0].RightCols[q].ID + "\">" + oJSON[0].RightCols[q].Name + "</option>";
              }
              else {
                  orderOptions += "<option value=\"" + oJSON[0].RightCols[q].ID + "\">" + oJSON[0].RightCols[q].Name + "</option>";
              }
          }
          output += "<div class=\"page-settings-field\"><label for=\"pageSettingsFieldRightCol\">Right Column:</label><select id=\"pageSettingsFieldRightCol\" onchange=\"UpdatePageSettingsRightCol('" + oJSON[0].pID + "', $(this).val()); return false;\">" + orderOptions + "</select></div>";

          orderOptions = "<option value=\"\"></option>";
          for (var r = 0; r < oJSON[0].Footers.length; r++) {
              if (oJSON[0].Footers[r].ID == oJSON[0].pFoot) {
                  orderOptions += "<option selected=\"selected\" value=\"" + oJSON[0].Footers[r].ID + "\">" + oJSON[0].Footers[r].Name + "</option>";
              }
              else {
                  orderOptions += "<option value=\"" + oJSON[0].Footers[r].ID + "\">" + oJSON[0].Footers[r].Name + "</option>";
              }
          }
          output += "<div class=\"page-settings-field\"><label for=\"pageSettingsFieldFooter\">Footer:</label><select id=\"pageSettingsFieldFooter\" onchange=\"UpdatePageSettingsFooter('" + oJSON[0].pID + "', $(this).val()); return false;\">" + orderOptions + "</select></div>";

          // Page versions
          output += "<strong class=\"navs-table-title\">Content Versions:</strong><table class=\"navs-table\"><thead><tr class=\"navs-table-header\"><th>Current Version</th><th>Created Date</th><th>Author</th><th></th></tr></thead><tbody>";
          for (var y = 0; y < oJSON[0].pVersions.length; y++) {
              output += "<tr" + (y % 2 == 0 ? " class=\"alt\"" : "") + "><td><input type=\"radio\"" + (oJSON[0].pVersions[y].ID == oJSON[0].pCurrentVersion ? " checked=\"checked\"" : "") + " name=\"pageSettingsVersion\" onclick=\"ChangePageVersion('" + oJSON[0].pID + "', '" + oJSON[0].pVersions[y].ID + "')\" /></td><td>" + oJSON[0].pVersions[y].CreatedDate + "</td><td>" + oJSON[0].pVersions[y].Author + "</td><td><a target=\"_blank\" href=\"/" + pagePath.replace(".aspx", ".preview") + "?ContentID=" + oJSON[0].pVersions[y].ID + "\">Preview</a></td></tr>";
          }
          output += "</tbody></table>";

          $('#explorer').html(output);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function SaveAndPublishPage(pageID) {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=SaveAndPublishPage"
      , type: "POST"
      , data: { "content": CKEDITOR.instances["pageEditor"].getData(), "pageID": pageID }
      , success: function (data) {
          handleSuccessMessages(data);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function SavePage(pageID) {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=SavePage"
      , type: "POST"
      , data: { "content": CKEDITOR.instances["pageEditor"].getData(), "pageID": pageID }
      , success: function (data) {
          handleSuccessMessages(data);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function EditPage(pagePath, pageName) {
    $('.loading').show();
    $('#pagePath').val(pagePath);
    $('#ribbon').children('a').remove();
    //$('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    //$('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditPage('" + pagePath + "', '" + pageName + "');return false;\"><span class=\"icon sprite-edit\"></span>" + pageName + "</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=EditPage"
      , type: "POST"
      , data: { "pagePath": pagePath }
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = "<a href=\"#\" title=\"Save\" class=\"button icon sprite-save\" onclick=\"SavePage('" + oJSON[0].PageID + "'); return false;\" />";
          if (oJSON[0].IsAdmin) {
              output += "<a href=\"#\" title=\"Save And Publish\" class=\"button icon sprite-savepublish\" onclick=\"if(confirm('Are you sure you want to replace the existing data?')) { SaveAndPublishPage('" + oJSON[0].PageID + "'); } return false;\" />";
          }
          $('#ribbon').prepend(output);
          output = '';
          output += "<br /><div id=\"pageEditor\">" + oJSON[0].Content + "</div>";
          $('#explorer').html(output);
          if (CKEDITOR.instances["pageEditor"]) {
              CKEDITOR.instances["pageEditor"].destroy(true);
          }

          CKEDITOR.replace('pageEditor');
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function LoadDirectory(path) {
    $('.loading').show();
    $('#path').val(path);
    $.ajax({
        url: "/lidocaine.ashx?f=LoadExplorer"
      , type: "POST"
      , data: { "path": (path == null ? "" : path), "depth": (path == null ? "0" : path.split('/').length) }
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = '';
          if (oJSON[0].IsAdmin) {
              $('.template-link').show();
              output += "<a href=\"#\" title=\"Add New Folder\" class=\"button icon sprite-newfolder\" onclick=\"$('#addfolder').dialog('open'); return false;\" /><a href=\"#\" title=\"Add New Page\" class=\"button icon sprite-newpage\" onclick=\"$('#addpage').dialog('open'); return false;\" /><a href=\"#\" title=\"Upload File(s)\" class=\"button icon sprite-download\" onclick=\"$('#addfile').dialog('open'); return false;\" />";
          }

          output += "<div class=\"breadcrumbs\"><a class=\"address\" href=\"#\" onclick=\"LoadDirectory();return false;\"><span class=\"icon sprite-globe\"></span>" + oJSON[0].N + "</a>";
          if (path != null && path != '') {
              var nodes = path.split('/');
              var rpath = '';
              for (var i = 0; i < nodes.length; i++) {
                  rpath += nodes[i];
                  output += "<a class=\"address\" href=\"#\" onclick=\"LoadDirectory('" + rpath + "');return false;\"><span class=\"icon sprite-folder\"></span>" + nodes[i] + "</a>";
                  rpath += "/";
              }
          }

          output += "</div>";
          $('#ribbon').html(output);

          output = '';
          if (oJSON[0].IsAdmin) {
              for (var j = 0; j < oJSON[0].Items.length; j++) {
                  //Source: 1=folder, 2=page, 3=file
                  switch (oJSON[0].Items[j].Source) {
                      case "0":
                          output += "<div class=\"item" + (j % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-delete\" onclick=\"deleteFolder('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Permissions\" class=\"button icon sprite-user\" onclick=\"permissionsFolder('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-rename\" onclick=\"renameFolder('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Open\" class=\"button icon sprite-open\" onclick=\"LoadDirectory('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Click To Open\" class=\"afilename\" onclick=\"LoadDirectory('" + oJSON[0].Items[j].Path + "'); return false;\"><span class=\"icon sprite-folder\"></span>" + oJSON[0].Items[j].Path.substring(oJSON[0].Items[j].Path.lastIndexOf('/') + 1) + "</a></div>";
                          break;
                      case "1":
                          output += "<div class=\"item" + (j % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-delete\" onclick=\"deletePage('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Permissions\" class=\"button icon sprite-user\" onclick=\"permissionsPage('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-rename\" onclick=\"renamePage('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" onclick=\"EditPageSettings('" + oJSON[0].Items[j].Path + "');return false;\" title=\"Settings\" class=\"button icon sprite-settings\" /><a href=\"#\" onclick=\"EditPage('" + oJSON[0].Items[j].Path + "', '" + oJSON[0].Items[j].Path.substring(oJSON[0].Items[j].Path.lastIndexOf('/') + 1) + "');return false;\" title=\"Edit\" class=\"button icon sprite-edit\" /><a href=\"/" + oJSON[0].Items[j].Path + "\" target=\"_blank\" class=\"button icon sprite-preview\" title=\"View\" /><a href=\"/" + oJSON[0].Items[j].Path + "\" target=\"_blank\" class=\"afilename\" title=\"Click To View\"><span class=\"icon sprite-new\"></span>" + oJSON[0].Items[j].Path.substring(oJSON[0].Items[j].Path.lastIndexOf('/') + 1) + "</a></div>";
                          break;
                      case "2":
                          output += "<div class=\"item" + (j % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-delete\" onclick=\"deleteFile('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Permissions\" class=\"button icon sprite-user\" onclick=\"permissionsFile('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-rename\" onclick=\"renameFile('" + oJSON[0].Items[j].Path + "'); return false;\" /><a href=\"/" + oJSON[0].Items[j].Path + "\" title=\"View\" class=\"button icon sprite-preview\" target=\"_blank\" /><a href=\"/" + oJSON[0].Items[j].Path + "\" title=\"Click To View\" class=\"afilename\" target=\"_blank\"><span class=\"icon sprite-new\"></span>" + oJSON[0].Items[j].Path.substring(oJSON[0].Items[j].Path.lastIndexOf('/') + 1) + "</a></div>";
                          break;
                  }
              }
          }
          else {
              for (var k = 0; k < oJSON[0].Items.length; k++) {
                  switch (oJSON[0].Items[k].Source) {
                      case "0":
                          output += "<div class=\"item" + (k % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Open\" class=\"button icon sprite-open\" onclick=\"LoadDirectory('" + oJSON[0].Items[k].Path + "'); return false;\" /><a href=\"#\" title=\"Click To Open\" class=\"afilename\" onclick=\"LoadDirectory('" + oJSON[0].Items[k].Path + "'); return false;\"><span class=\"icon sprite-folder\"></span>" + oJSON[0].Items[k].Path.substring(oJSON[0].Items[k].Path.lastIndexOf('/') + 1) + "</a></div>";
                          break;
                      case "1":
                          output += "<div class=\"item" + (k % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" onclick=\"EditPage('" + oJSON[0].Items[k].Path + "', '" + oJSON[0].Items[k].Path.substring(oJSON[0].Items[k].Path.lastIndexOf('/') + 1) + "');return false;\" title=\"Edit\" class=\"button icon sprite-edit\" /><a href=\"/" + oJSON[0].Items[k].Path + "\" target=\"_blank\" class=\"button icon sprite-preview\" title=\"View\" /><a href=\"/" + oJSON[0].Items[k].Path + "\" target=\"_blank\" class=\"afilename\" title=\"Click To View\"><span class=\"icon sprite-new\"></span>" + oJSON[0].Items[k].Path.substring(oJSON[0].Items[k].Path.lastIndexOf('/') + 1) + "</a></div>";
                          break;
                      case "2":
                          output += "<div class=\"item" + (k % 2 == 0 ? " alt" : "") + "\"><a href=\"/" + oJSON[0].Items[k].Path + "\" title=\"View\" class=\"button icon sprite-preview\" target=\"_blank\" /><a href=\"/" + oJSON[0].Items[k].Path + "\" title=\"Click To View\" class=\"afilename\" target=\"_blank\"><span class=\"icon sprite-new\"></span>" + oJSON[0].Items[k].Path.substring(oJSON[0].Items[k].Path.lastIndexOf('/') + 1) + "</a></div>";
                          break;
                  }
              }
          }

          $('#explorer').html(output);
          $('.loading').hide();
          if (oJSON[0].IsAdmin) {
              LoadUsersAndRoles();
          }
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function SaveAndPublishRightCol() {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=UpdateTemplate"
      , type: "POST"
      , data: { "content": CKEDITOR.instances["rightcolEditor"].getData(), "templateID": $('#pagePath').val() }
      , success: function (data) {
          handleSuccessMessages(data);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function EditRightCol(RightColID, RightColName) {
    $('.loading').show();
    $('#pagePath').val(RightColID);
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditRightCol('" + RightColID + "', '" + RightColName + "');return false;\"><span class=\"icon sprite-new\"></span>" + RightColName + "</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=EditTemplate"
      , type: "POST"
      , data: { "templateID": RightColID }
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = ''; /*"<a href=\"#\" title=\"Save\" class=\"button icon sprite-save\" onclick=\"SaveHeader(); return false;\" />";*/ // There is no versioning for headers, so this is not applicable.
          output += "<a href=\"#\" title=\"Save And Publish\" class=\"button icon sprite-savepublish\" onclick=\"if(confirm('Are you sure you want to replace the existing data?')) { SaveAndPublishRightCol(); } return false;\" />";
          $('#ribbon').prepend(output);
          output = '';
          for (var i = 0; i < oJSON[0].Pages.length; i++) {
              output += "<li>" + oJSON[0].Pages[i].Name + "</li>";
          }

          if (output != '') {
              output = "<strong>Pages using this right column:</strong><br /><ul>" + output + "</ul>";
          }
          else {
              output = "<strong>There are no pages using this right column.</strong><br />";
          }

          output += "<br /><div id=\"rightcolEditor\">" + oJSON[0].Content + "</div>";
          $('#explorer').html(output);
          if (CKEDITOR.instances["rightcolEditor"]) {
              CKEDITOR.instances["rightcolEditor"].destroy(true);
          }

          CKEDITOR.replace('rightcolEditor');
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function LoadRightCols() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadRightCols();return false;\"><span class=\"icon sprite-folder\"></span>Right Columns</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=LoadTemplates"
      , data: { "area": TemplateSections.RightCol }
      , type: "POST"
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = "<a href=\"#\" title=\"Add New Right Column\" class=\"button icon sprite-newpage\" onclick=\"$('#addrightcol').dialog('open'); return false;\" />";
          $('#ribbon').prepend(output);

          output = '';
          for (var i = 0; i < oJSON.length; i++) {
              output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-delete\" onclick=\"deleteRightCol('" + oJSON[i].ID + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-rename\" onclick=\"renameRightCol('" + oJSON[i].ID + "', '" + oJSON[i].Name + "'); return false;\" /><a href=\"#\" onclick=\"EditRightCol('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"button icon sprite-edit\" /><a href=\"#\" onclick=\"EditRightCol('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Click To Edit\" class=\"afilename\"><span class=\"icon sprite-new\"></span>" + oJSON[i].Name + "</a></div>";
          }

          $('#explorer').html(output);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function SaveAndPublishLeftNav() {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=UpdateTemplate"
      , type: "POST"
      , data: { "content": CKEDITOR.instances["leftnavEditor"].getData(), "templateID": $('#pagePath').val() }
      , success: function (data) {
          handleSuccessMessages(data);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function EditLeftNav(LeftNavID, LeftNavName) {
    $('.loading').show();
    $('#pagePath').val(LeftNavID);
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditLeftNav('" + LeftNavID + "', '" + LeftNavName + "');return false;\"><span class=\"icon sprite-new\"></span>" + LeftNavName + "</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=EditTemplate"
      , type: "POST"
      , data: { "templateID": LeftNavID }
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = ''; /*"<a href=\"#\" title=\"Save\" class=\"button icon sprite-save\" onclick=\"SaveHeader(); return false;\" />";*/ // There is no versioning for headers, so this is not applicable.
          output += "<a href=\"#\" title=\"Save And Publish\" class=\"button icon sprite-savepublish\" onclick=\"if(confirm('Are you sure you want to replace the existing data?')) { SaveAndPublishLeftNav(); } return false;\" />";
          $('#ribbon').prepend(output);
          output = '';
          for (var i = 0; i < oJSON[0].Pages.length; i++) {
              output += "<li>" + oJSON[0].Pages[i].Name + "</li>";
          }

          if (output != '') {
              output = "<strong>Pages using this left column:</strong><br /><ul>" + output + "</ul>";
          }
          else {
              output = "<strong>There are no pages using this left column.</strong><br />";
          }

          output += "<br /><div id=\"leftnavEditor\">" + oJSON[0].Content + "</div>";
          $('#explorer').html(output);
          if (CKEDITOR.instances["leftnavEditor"]) {
              CKEDITOR.instances["leftnavEditor"].destroy(true);
          }

          CKEDITOR.replace('leftnavEditor');
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function LoadLeftNavs() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadLeftNavs();return false;\"><span class=\"icon sprite-folder\"></span>Left Columns</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=LoadTemplates"
      , data: { "area": TemplateSections.LeftNav }
      , type: "POST"
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = "<a href=\"#\" title=\"Add New Left Column\" class=\"button icon sprite-newpage\" onclick=\"$('#addleftnav').dialog('open'); return false;\" />";
          $('#ribbon').prepend(output);

          output = '';
          for (var i = 0; i < oJSON.length; i++) {
              output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-delete\" onclick=\"deleteLeftNav('" + oJSON[i].ID + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-rename\" onclick=\"renameLeftNav('" + oJSON[i].ID + "', '" + oJSON[i].Name + "'); return false;\" /><a href=\"#\" onclick=\"EditLeftNav('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"button icon sprite-edit\" /><a href=\"#\" onclick=\"EditLeftNav('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Click To Edit\" class=\"afilename\"><span class=\"icon sprite-new\"></span>" + oJSON[i].Name + "</a></div>";
          }

          $('#explorer').html(output);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function SaveAndPublishTopNav() {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=UpdateTemplate"
      , type: "POST"
      , data: { "content": CKEDITOR.instances["topnavEditor"].getData(), "templateID": $('#pagePath').val() }
      , success: function (data) {
          handleSuccessMessages(data);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function EditTopNav(TopNavID, TopNavName) {
    $('.loading').show();
    $('#pagePath').val(TopNavID);
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditTopNav('" + TopNavID + "', '" + TopNavName + "');return false;\"><span class=\"icon sprite-new\"></span>" + TopNavName + "</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=EditTemplate"
      , type: "POST"
      , data: { "templateID": TopNavID }
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = ''; /*"<a href=\"#\" title=\"Save\" class=\"button icon sprite-save\" onclick=\"SaveHeader(); return false;\" />";*/ // There is no versioning for headers, so this is not applicable.
          output += "<a href=\"#\" title=\"Save And Publish\" class=\"button icon sprite-savepublish\" onclick=\"if(confirm('Are you sure you want to replace the existing data?')) { SaveAndPublishTopNav(); } return false;\" />";
          $('#ribbon').prepend(output);
          output = '';
          for (var i = 0; i < oJSON[0].Pages.length; i++) {
              output += "<li>" + oJSON[0].Pages[i].Name + "</li>";
          }

          if (output != '') {
              output = "<strong>Pages using this top navigation:</strong><br /><ul>" + output + "</ul>";
          }
          else {
              output = "<strong>There are no pages using this top navigation.</strong><br />";
          }

          output += "<br /><div id=\"topnavEditor\">" + oJSON[0].Content + "</div>";
          $('#explorer').html(output);
          if (CKEDITOR.instances["topnavEditor"]) {
              CKEDITOR.instances["topnavEditor"].destroy(true);
          }

          CKEDITOR.replace('topnavEditor');
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function LoadTopNavs() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadTopNavs();return false;\"><span class=\"icon sprite-folder\"></span>Top Navigations</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=LoadTemplates"
      , data: { "area": TemplateSections.TopNav }
      , type: "POST"
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = "<a href=\"#\" title=\"Add New Top Navigation\" class=\"button icon sprite-newpage\" onclick=\"$('#addtopnav').dialog('open'); return false;\" />";
          $('#ribbon').prepend(output);

          output = '';
          for (var i = 0; i < oJSON.length; i++) {
              output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-delete\" onclick=\"deleteTopNav('" + oJSON[i].ID + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-rename\" onclick=\"renameTopNav('" + oJSON[i].ID + "', '" + oJSON[i].Name + "'); return false;\" /><a href=\"#\" onclick=\"EditTopNav('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"button icon sprite-edit\" /><a href=\"#\" onclick=\"EditTopNav('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Click To Edit\" class=\"afilename\"><span class=\"icon sprite-new\"></span>" + oJSON[i].Name + "</a></div>";
          }

          $('#explorer').html(output);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function LoadMDeveloperControls() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadMDeveloperControls();return false;\"><span class=\"icon sprite-folder\"></span>Main Column DeveloperControls</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=LoadMainColumnDeveloperControls"
      , type: "GET"
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = "<a href=\"#\" title=\"Register New Main Column DeveloperControl\" class=\"button icon sprite-newpage\" onclick=\"$('#addmdevelopercontrol').dialog('open'); return false;\" />";
          $('#ribbon').prepend(output);

          output = '';
          for (var i = 0; i < oJSON.length; i++) {
              output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-delete\" onclick=\"deleteMDeveloperControl('" + oJSON[i].ID + "'); return false;\" /><a href=\"#\" onclick=\"describeMDeveloperControl('" + oJSON[i].ID + "');return false;\" title=\"View Description\" class=\"button icon sprite-preview\" /><a href=\"#\" onclick=\"describeMDeveloperControl('" + oJSON[i].ID + "');return false;\" title=\"Click To View Description\" class=\"afilename\"><span class=\"icon sprite-new\"></span><strong>" + oJSON[i].Name + "</strong> - " + oJSON[i].Path + "</a></div>";
          }

          $('#explorer').html(output);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function SaveAndPublishHeader() {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=UpdateTemplate"
      , type: "POST"
      , data: { "content": CKEDITOR.instances["headerEditor"].getData(), "templateID": $('#pagePath').val() }
      , success: function (data) {
          handleSuccessMessages(data);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function EditHeader(headerID, headerName) {
    $('.loading').show();
    $('#pagePath').val(headerID);
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditHeader('" + headerID + "', '" + headerName + "');return false;\"><span class=\"icon sprite-new\"></span>" + headerName + "</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=EditTemplate"
      , type: "POST"
      , data: { "templateID": headerID }
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = ''; /*"<a href=\"#\" title=\"Save\" class=\"button icon sprite-save\" onclick=\"SaveHeader(); return false;\" />";*/ // There is no versioning for headers, so this is not applicable.
          output += "<a href=\"#\" title=\"Save And Publish\" class=\"button icon sprite-savepublish\" onclick=\"if(confirm('Are you sure you want to replace the existing data?')) { SaveAndPublishHeader(); } return false;\" />";
          $('#ribbon').prepend(output);
          output = '';
          for (var i = 0; i < oJSON[0].Pages.length; i++) {
              output += "<li>" + oJSON[0].Pages[i].Name + "</li>";
          }

          if (output != '') {
              output = "<strong>Pages using this header:</strong><br /><ul>" + output + "</ul>";
          }
          else {
              output = "<strong>There are no pages using this header.</strong><br />";
          }

          output += "<br /><div id=\"headerEditor\">" + oJSON[0].Content + "</div>";
          $('#explorer').html(output);
          if (CKEDITOR.instances["headerEditor"]) {
              CKEDITOR.instances["headerEditor"].destroy(true);
          }

          CKEDITOR.replace('headerEditor');
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function LoadHeaders() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadHeaders();return false;\"><span class=\"icon sprite-folder\"></span>Headers</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=LoadTemplates"
      , data: { "area": TemplateSections.Header }
      , type: "POST"
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = "<a href=\"#\" title=\"Add New Header\" class=\"button icon sprite-newpage\" onclick=\"$('#addheader').dialog('open'); return false;\" />";
          $('#ribbon').prepend(output);

          output = '';
          for (var i = 0; i < oJSON.length; i++) {
              output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-delete\" onclick=\"deleteHeader('" + oJSON[i].ID + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-rename\" onclick=\"renameHeader('" + oJSON[i].ID + "', '" + oJSON[i].Name + "'); return false;\" /><a href=\"#\" onclick=\"EditHeader('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"button icon sprite-edit\" /><a href=\"#\" onclick=\"EditHeader('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Click To Edit\" class=\"afilename\"><span class=\"icon sprite-new\"></span>" + oJSON[i].Name + "</a></div>";
          }

          $('#explorer').html(output);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function SaveAndPublishFooter() {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=UpdateTemplate"
      , type: "POST"
      , data: { "content": CKEDITOR.instances["footerEditor"].getData(), "templateID": $('#pagePath').val() }
      , success: function (data) {
          handleSuccessMessages(data);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function EditFooter(footerID, footerName) {
    $('.loading').show();
    $('#pagePath').val(footerID);
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditFooter('" + footerID + "', '" + footerName + "');return false;\"><span class=\"icon sprite-new\"></span>" + footerName + "</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=EditTemplate"
      , type: "POST"
      , data: { "templateID": footerID }
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = ''; /*"<a href=\"#\" title=\"Save\" class=\"button icon sprite-save\" onclick=\"SaveFooter(); return false;\" />";*/ // There is no versioning for footers, so this is not applicable.
          output += "<a href=\"#\" title=\"Save And Publish\" class=\"button icon sprite-savepublish\" onclick=\"if(confirm('Are you sure you want to replace the existing data?')) { SaveAndPublishFooter(); } return false;\" />";
          $('#ribbon').prepend(output);
          output = '';
          for (var i = 0; i < oJSON[0].Pages.length; i++) {
              output += "<li>" + oJSON[0].Pages[i].Name + "</li>";
          }

          if (output != '') {
              output = "<strong>Pages using this footer:</strong><br /><ul>" + output + "</ul>";
          }
          else {
              output = "<strong>There are no pages using this footer.</strong><br />";
          }

          output += "<br /><div id=\"footerEditor\">" + oJSON[0].Content + "</div>";
          $('#explorer').html(output);
          if (CKEDITOR.instances["footerEditor"]) {
              CKEDITOR.instances["footerEditor"].destroy(true);
          }

          CKEDITOR.replace('footerEditor');
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function LoadFooters() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadFooters();return false;\"><span class=\"icon sprite-folder\"></span>Footers</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=LoadTemplates"
      , data: { "area": TemplateSections.Footer }
      , type: "POST"
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = "<a href=\"#\" title=\"Add New Footer\" class=\"button icon sprite-newpage\" onclick=\"$('#addfooter').dialog('open'); return false;\" />";
          $('#ribbon').prepend(output);

          output = '';
          for (var i = 0; i < oJSON.length; i++) {
              output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-delete\" onclick=\"deleteFooter('" + oJSON[i].ID + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-rename\" onclick=\"renameFooter('" + oJSON[i].ID + "', '" + oJSON[i].Name + "'); return false;\" /><a href=\"#\" onclick=\"EditFooter('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"button icon sprite-edit\" /><a href=\"#\" onclick=\"EditFooter('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Click To Edit\" class=\"afilename\"><span class=\"icon sprite-new\"></span>" + oJSON[i].Name + "</a></div>";
          }

          $('#explorer').html(output);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function SaveAndPublishCss() {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=UpdateCss"
      , type: "POST"
      , data: { "content": codeMirrorObject.getValue(), "cssID": $('#pagePath').val() }
      , success: function (data) {
          handleSuccessMessages(data);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function EditCss(cssID, cssName) {
    $('.loading').show();
    $('#pagePath').val(cssID);
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditCss('" + cssID + "', '" + cssName + "');return false;\"><span class=\"icon sprite-new\"></span>" + cssName + "</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=EditCss"
      , type: "POST"
      , data: { "cssID": cssID }
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = ''; /*"<a href=\"#\" title=\"Save\" class=\"button icon sprite-save\" onclick=\"SaveCss(); return false;\" />";*/ // There is no versioning for scripts, so this is not applicable.
          output += "<a href=\"#\" title=\"Save And Publish\" class=\"button icon sprite-savepublish\" onclick=\"if(confirm('Are you sure you want to replace the existing data?')) { SaveAndPublishCss(); } return false;\" />";
          $('#ribbon').prepend(output);
          output = '';
          for (var i = 0; i < oJSON[0].Pages.length; i++) {
              output += "<li>" + oJSON[0].Pages[i].Name + "</li>";
          }

          if (output != '') {
              output = "<strong>Pages using this CSS script:</strong><br /><ul>" + output + "</ul>";
          }
          else {
              output = "<strong>There are no pages using this CSS script.</strong><br />";
          }

          output += "<br /><textarea id=\"cssEditor\">" + oJSON[0].Content + "</textarea>";
          $('#explorer').html(output);
          codeMirrorObject = CodeMirror.fromTextArea(document.getElementById("cssEditor"), { mode: "css", lineNumbers: true });
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function LoadCss() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadCss();return false;\"><span class=\"icon sprite-folder\"></span>CSS Scripts</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=LoadCss"
      , type: "GET"
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = "<a href=\"#\" title=\"Add New CSS Script\" class=\"button icon sprite-newpage\" onclick=\"$('#addcss').dialog('open'); return false;\" />";
          $('#ribbon').prepend(output);
          output = '';
          for (var i = 0; i < oJSON.length; i++) {
              output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-delete\" onclick=\"deleteCss('" + oJSON[i].ID + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-rename\" onclick=\"renameCss('" + oJSON[i].ID + "', '" + oJSON[i].Name + "'); return false;\" /><a href=\"#\" onclick=\"EditCss('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"button icon sprite-edit\" /><a href=\"#\" onclick=\"EditCss('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Click To Edit\" class=\"afilename\"><span class=\"icon sprite-new\"></span>" + oJSON[i].Name + "</a></div>";
          }

          $('#explorer').html(output);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function SaveAndPublishJs() {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=UpdateJs"
      , type: "POST"
      , data: { "content": codeMirrorObject.getValue(), "jsID": $('#pagePath').val() }
      , success: function (data) {
          handleSuccessMessages(data);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function EditJs(jsID, jsName) {
    $('.loading').show();
    $('#pagePath').val(jsID);
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 2) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"EditJs('" + jsID + "', '" + jsName + "');return false;\"><span class=\"icon sprite-new\"></span>" + jsName + "</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=EditJs"
      , type: "POST"
      , data: { "jsID": jsID }
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = ''; /*"<a href=\"#\" title=\"Save\" class=\"button icon sprite-save\" onclick=\"SaveJs(); return false;\" />";*/ // There is no versioning for scripts, so this is not applicable.
          output += "<a href=\"#\" title=\"Save And Publish\" class=\"button icon sprite-savepublish\" onclick=\"if(confirm('Are you sure you want to replace the existing data?')) { SaveAndPublishJs(); } return false;\" />";
          $('#ribbon').prepend(output);
          output = '';
          for (var i = 0; i < oJSON[0].Pages.length; i++) {
              output += "<li>" + oJSON[0].Pages[i].Name + "</li>";
          }

          if (output != '') {
              output = "<strong>Pages using this Javascript:</strong><br /><ul>" + output + "</ul>";
          }
          else {
              output = "<strong>There are no pages using this Javascript.</strong><br />";
          }

          output += "<br /><textarea id=\"jsEditor\">" + oJSON[0].Content + "</textarea>";
          $('#explorer').html(output);
          codeMirrorObject = CodeMirror.fromTextArea(document.getElementById("jsEditor"), { mode: "javascript", lineNumbers: true });
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function LoadJs() {
    $('.loading').show();
    $('#ribbon').children('a').remove();
    $('.breadcrumbs').children().each(function (index) { if (index > 1) $(this).remove(); });
    $('.breadcrumbs').append("<a class=\"address\" href=\"#\" onclick=\"LoadJs();return false;\"><span class=\"icon sprite-folder\"></span>Javascripts</a>");
    $.ajax({
        url: "/lidocaine.ashx?f=LoadJs"
      , type: "GET"
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var output = "<a href=\"#\" title=\"Add New Javascript\" class=\"button icon sprite-newpage\" onclick=\"$('#addjs').dialog('open'); return false;\" />";
          $('#ribbon').prepend(output);

          output = '';
          for (var i = 0; i < oJSON.length; i++) {
              output += "<div class=\"item" + (i % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Delete\" class=\"button icon sprite-delete\" onclick=\"deleteJs('" + oJSON[i].ID + "'); return false;\" /><a href=\"#\" title=\"Rename\" class=\"button icon sprite-rename\" onclick=\"renameJs('" + oJSON[i].ID + "', '" + oJSON[i].Name + "'); return false;\" /><a href=\"#\" onclick=\"EditJs('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Edit\" class=\"button icon sprite-edit\" /><a href=\"#\" onclick=\"EditJs('" + oJSON[i].ID + "', '" + oJSON[i].Name + "');return false;\" title=\"Click To Edit\" class=\"afilename\"><span class=\"icon sprite-new\"></span>" + oJSON[i].Name + "</a></div>";
          }

          $('#explorer').html(output);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function LoadUsersAndRoles() {
    $.ajax({
        url: "/lidocaine.ashx?f=LoadUsers"
      , type: "GET"
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var users = "";
          for (var i = 0; i < oJSON.length; i++) {
              users += "<option value=\"" + oJSON[i].UserID + "\">" + oJSON[i].UserName + "</option>";
          }
          $("#UserList").html(users);
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });

    $.ajax({
        url: "/lidocaine.ashx?f=LoadRoles"
      , type: "GET"
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var groups = "";
          for (var i = 0; i < oJSON.length; i++) {
              groups += "<option value=\"" + oJSON[i].RoleID + "\">" + oJSON[i].RoleName + "</option>";
          }
          $("#RoleList").html(groups);
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function RenameFile(dialog) {
    var fileName = $("#RenameFile").val();
    if (fileName.length < 1) {
        $('#renameFileValidation').html("A new file name cannot be blank.");
    }
    else if (!fileName.match(/^[.\w]+$/)) {
        $('#renameFileValidation').html("Please use only alphanumeric characters and \".\".");
    }
    else {
        $(".loading").show();
        $.ajax({
            url: "/lidocaine.ashx?f=RenameFile"
          , type: "POST"
          , data: { "path": $('#path').val(), "name": fileName, "existingPath": $('#pagePath').val() }
          , success: function (data) {
              dialog.dialog('close');
              $("#RenameFile").val("");
              handleSuccessMessages(data);
              LoadDirectory($('#path').val());
              $('.loading').hide();
          }
          , error: function (data) {
              handleErrorMessages(data.responseText);
          }
        });
    }
}

function RenamePage(dialog) {
    var pageName = $("#RenamePage").val();
    if (pageName.length < 1) {
        $('#renamePageValidation').html("A new page name cannot be blank.");
    }
    else if (!pageName.match(/^[\w]+$/)) {
        $('#renamePageValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            url: "/lidocaine.ashx?f=RenamePage"
          , type: "POST"
          , data: { "path": $('#path').val(), "name": pageName, "existingPath": $('#pagePath').val() }
          , success: function (data) {
              dialog.dialog('close');
              $("#RenamePage").val("");
              handleSuccessMessages(data);
              LoadDirectory($('#path').val());
              $('.loading').hide();
          }
          , error: function (data) {
              handleErrorMessages(data.responseText);
          }
        });
    }
}

function RenameFolder(dialog) {
    var folderName = $("#RenameFolder").val();
    if (folderName.length < 1) {
        $('#renameFolderValidation').html("A new folder name cannot be blank.");
    }
    else if (!folderName.match(/^[\w]+$/)) {
        $('#renameFolderValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            url: "/lidocaine.ashx?f=RenameFolder"
          , type: "POST"
          , data: { "path": $('#path').val(), "name": folderName, "existingPath": $('#pagePath').val() }
          , success: function (data) {
              dialog.dialog('close');
              $("#RenameFolder").val("");
              handleSuccessMessages(data);
              LoadDirectory($('#path').val());
              $('.loading').hide();
          }
          , error: function (data) {
              handleErrorMessages(data.responseText);
          }
        });
    }
}

function RenameRightCol(dialog) {
    var LeftNavName = $("#RenameRightCol").val();
    if (LeftNavName.length < 1) {
        $('#renameRightColValidation').html("A new right column name cannot be blank.");
    }
    else if (!LeftNavName.match(/^[\w]+$/)) {
        $('#renameRightColValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=RenameTemplate",
            data: "name=" + LeftNavName + "&existingName=" + $('#path').val() + "&templateID=" + $('#pagePath').val(),
            success: function (data) {
                dialog.dialog('close');
                $("#RenameRightCol").val("");
                handleSuccessMessages(data);
                LoadRightCols();
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function RenameLeftNav(dialog) {
    var LeftNavName = $("#RenameLeftNav").val();
    if (LeftNavName.length < 1) {
        $('#renameLeftNavValidation').html("A new left column name cannot be blank.");
    }
    else if (!LeftNavName.match(/^[\w]+$/)) {
        $('#renameLeftNavValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=RenameTemplate",
            data: "name=" + LeftNavName + "&existingName=" + $('#path').val() + "&templateID=" + $('#pagePath').val(),
            success: function (data) {
                dialog.dialog('close');
                $("#RenameLeftNav").val("");
                handleSuccessMessages(data);
                LoadLeftNavs();
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function RenameTopNav(dialog) {
    var TopNavName = $("#RenameTopNav").val();
    if (TopNavName.length < 1) {
        $('#renameTopNavValidation').html("A new top navigation name cannot be blank.");
    }
    else if (!TopNavName.match(/^[\w]+$/)) {
        $('#renameTopNavValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=RenameTemplate",
            data: "name=" + headerName + "&existingName=" + $('#path').val() + "&templateID=" + $('#pagePath').val(),
            success: function (data) {
                dialog.dialog('close');
                $("#RenameTopNav").val("");
                handleSuccessMessages(data);
                LoadTopNavs();
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function RenameHeader(dialog) {
    var headerName = $("#RenameHeader").val();
    if (headerName.length < 1) {
        $('#renameHeaderValidation').html("A new header name cannot be blank.");
    }
    else if (!headerName.match(/^[\w]+$/)) {
        $('#renameHeaderValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=RenameTemplate",
            data: "name=" + headerName + "&existingName=" + $('#path').val() + "&templateID=" + $('#pagePath').val(),
            success: function (data) {
                dialog.dialog('close');
                $("#RenameHeader").val("");
                handleSuccessMessages(data);
                LoadHeaders();
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function RenameFooter(dialog) {
    var footerName = $("#RenameFooter").val();
    if (footerName.length < 1) {
        $('#renameFooterValidation').html("A new footer name cannot be blank.");
    }
    else if (!footerName.match(/^[\w]+$/)) {
        $('#renameFooterValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=RenameTemplate",
            data: "name=" + footerName + "&existingName=" + $('#path').val() + "&templateID=" + $('#pagePath').val(),
            success: function (data) {
                dialog.dialog('close');
                $("#RenameFooter").val("");
                handleSuccessMessages(data);
                LoadFooters();
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function RenameCss(dialog) {
    var cssName = $("#RenameCss").val();
    if (cssName.length < 1) {
        $('#renameCssValidation').html("A new CSS script name cannot be blank.");
    }
    else if (!cssName.match(/^[\w]+$/)) {
        $('#renameCssValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=RenameCss",
            data: "name=" + cssName + "&existingName=" + $('#path').val() + "&scriptID=" + $('#pagePath').val(),
            success: function (data) {
                dialog.dialog('close');
                $("#RenameCss").val("");
                handleSuccessMessages(data);
                LoadCss();
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function RenameJs(dialog) {
    var jsName = $("#RenameJs").val();
    if (jsName.length < 1) {
        $('#renameJsValidation').html("A new Javascript name cannot be blank.");
    }
    else if (!jsName.match(/^[\w]+$/)) {
        $('#renameJsValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=RenameJs",
            data: "name=" + jsName + "&existingName=" + $('#path').val() + "&scriptID=" + $('#pagePath').val(),
            success: function (data) {
                dialog.dialog('close');
                $("#RenameJs").val("");
                handleSuccessMessages(data);
                LoadJs();
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function DeleteFile(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteFile",
        data: "path=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            handleSuccessMessages(data);
            LoadDirectory($('#path').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function DeletePage(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeletePage",
        data: "path=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            handleSuccessMessages(data);
            LoadDirectory($('#path').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function DeleteFolder(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteFolder",
        data: "path=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            handleSuccessMessages(data);
            LoadDirectory($('#path').val());
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function DeleteMDeveloperControl(dialog) {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=DeleteMainColumnDeveloperControl"
      , type: "POST"
      , data: { "developerControlID": $('#pagePath').val() }
      , success: function (data) {
          dialog.dialog('close');
          $("#pagePath").val("");
          handleSuccessMessages(data);
          LoadMDeveloperControls();
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function DeleteRightCol(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteTemplate",
        data: "templateID=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            handleSuccessMessages(data);
            LoadRightCols();
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function DeleteLeftNav(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteTemplate",
        data: "templateID=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            handleSuccessMessages(data);
            LoadLeftNavs();
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function DeleteTopNav(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteTemplate",
        data: "templateID=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            handleSuccessMessages(data);
            LoadTopNavs();
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function DeleteHeader(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteTemplate",
        data: "templateID=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            handleSuccessMessages(data);
            LoadHeaders();
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function DeleteFooter(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteTemplate",
        data: "templateID=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            handleSuccessMessages(data);
            LoadFooters();
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function DeleteCss(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteCss",
        data: "scriptID=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            handleSuccessMessages(data);
            LoadCss();
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function DeleteJs(dialog) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=DeleteJs",
        data: "scriptID=" + $('#pagePath').val(),
        success: function (data) {
            dialog.dialog('close');
            $("#pagePath").val("");
            handleSuccessMessages(data);
            LoadJs();
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function AddPage(dialog) {
    var pageName = $("#NewPage").val();
    if (pageName.length < 1) {
        $('#newPageValidation').html("A new page name cannot be blank.");
    }
    else if (!pageName.match(/^[\w]+$/)) {
        $('#newPageValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddPage",
            data: "path=" + $('#path').val() + "&name=" + pageName,
            success: function (data) {
                dialog.dialog('close');
                $("#NewPage").val("");
                handleSuccessMessages(data);
                LoadDirectory($('#path').val());
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function AddFolder(dialog) {
    var folderName = $("#NewFolder").val();
    if (folderName.length < 1) {
        $('#newFolderValidation').html("A new folder name cannot be blank.");
    }
    else if (!folderName.match(/^[\w]+$/)) {
        $('#newFolderValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddFolder",
            data: "path=" + $('#path').val() + "&name=" + folderName,
            success: function (data) {
                dialog.dialog('close');
                $("#NewFolder").val("");
                handleSuccessMessages(data);
                LoadDirectory($('#path').val());
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function AddMDeveloperControl(dialog) {
    $('#newMDeveloperControlValidation').html("");
    var developerControlName = $("#NewMDeveloperControl").val();
    var developerControlPath = $("#NewMDeveloperControlPath").val();
    var developerControlDescription = $("#NewMDeveloperControlDescription").val();
    if (developerControlName.length < 1) {
        $('#newMDeveloperControlValidation').html("A new main column DeveloperControl name cannot be blank.");
    }
    else if (!developerControlName.match(/^[ \w\.]+$/)) {
        $('#newMDeveloperControlValidation').html("Please use only alphanumeric characters.");
    }

    if (developerControlPath.length < 1) {
        $('#newMDeveloperControlValidation').html("A new main column DeveloperControl path cannot be blank.");
    }

    if ($('#newMDeveloperControlValidation').html() == "") {
        $(".loading").show();
        $.ajax({
            url: "/lidocaine.ashx?f=AddMainColumnDeveloperControl"
          , type: "POST"
          , data: { "name": developerControlName, "path": developerControlPath, "description": developerControlDescription }
          , success: function (data) {
              dialog.dialog('close');
              $("#NewMDeveloperControl").val("");
              $("#NewMDeveloperControlPath").val("");
              $("#NewMDeveloperControlDescription").val("");
              handleSuccessMessages(data);
              LoadMDeveloperControls();
              $('.loading').hide();
          }
          , error: function (data) {
              handleErrorMessages(data.responseText);
          }
        });
    }
}

function AddRightCol(dialog) {
    var LeftNavName = $("#NewRightCol").val();
    if (LeftNavName.length < 1) {
        $('#newRightColValidation').html("A new right column name cannot be blank.");
    }
    else if (!LeftNavName.match(/^[\w]+$/)) {
        $('#newRightColValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddTemplate",
            data: { "name": LeftNavName, "area": TemplateSections.RightCol },
            success: function (data) {
                dialog.dialog('close');
                $("#NewLeftNav").val("");
                handleSuccessMessages(data);
                LoadRightCols();
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function AddLeftNav(dialog) {
    var LeftNavName = $("#NewLeftNav").val();
    if (LeftNavName.length < 1) {
        $('#newLeftNavValidation').html("A new left column name cannot be blank.");
    }
    else if (!LeftNavName.match(/^[\w]+$/)) {
        $('#newLeftNavValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddTemplate",
            data: { "name": LeftNavName, "area": TemplateSections.LeftNav },
            success: function (data) {
                dialog.dialog('close');
                $("#NewLeftNav").val("");
                handleSuccessMessages(data);
                LoadLeftNavs();
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function AddTopNav(dialog) {
    var TopNavName = $("#NewTopNav").val();
    if (TopNavName.length < 1) {
        $('#newTopNavValidation').html("A new top navigation name cannot be blank.");
    }
    else if (!TopNavName.match(/^[\w]+$/)) {
        $('#newTopNavValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddTemplate",
            data: { "name": TopNavName, "area": TemplateSections.TopNav },
            success: function (data) {
                dialog.dialog('close');
                $("#NewTopNav").val("");
                handleSuccessMessages(data);
                LoadTopNavs();
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function AddHeader(dialog) {
    var headerName = $("#NewHeader").val();
    if (headerName.length < 1) {
        $('#newHeaderValidation').html("A new header name cannot be blank.");
    }
    else if (!headerName.match(/^[\w]+$/)) {
        $('#newHeaderValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddTemplate",
            data: { "name": headerName, "area": TemplateSections.Header },
            success: function (data) {
                dialog.dialog('close');
                $("#NewHeader").val("");
                handleSuccessMessages(data);
                LoadHeaders();
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function AddFooter(dialog) {
    var footerName = $("#NewFooter").val();
    if (footerName.length < 1) {
        $('#newFooterValidation').html("A new footer name cannot be blank.");
    }
    else if (!footerName.match(/^[\w]+$/)) {
        $('#newFooterValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddTemplate",
            data: { "name": footerName, "area": TemplateSections.Footer },
            success: function (data) {
                dialog.dialog('close');
                $("#NewFooter").val("");
                handleSuccessMessages(data);
                LoadFooters();
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function AddCss(dialog) {
    var cssName = $("#NewCss").val();
    if (cssName.length < 1) {
        $('#newCssValidation').html("A new CSS script name cannot be blank.");
    }
    else if (!cssName.match(/^[\w]+$/)) {
        $('#newCssValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            url: "/lidocaine.ashx?f=AddCss"
          , type: "POST"
          , data: { "name": cssName }
          , success: function (data) {
              dialog.dialog('close');
              $("#NewCss").val("");
              handleSuccessMessages(data);
              LoadCss();
              $('.loading').hide();
          }
          , error: function (data) {
              handleErrorMessages(data.responseText);
          }
        });
    }
}

function AddJs(dialog) {
    var jsName = $("#NewJs").val();
    if (jsName.length < 1) {
        $('#newJsValidation').html("A new Javascript name cannot be blank.");
    }
    else if (!jsName.match(/^[\w]+$/)) {
        $('#newJsValidation').html("Please use only alphanumeric characters.");
    }
    else {
        $(".loading").show();
        $.ajax({
            type: "POST",
            url: "/lidocaine.ashx?f=AddJs",
            data: "name=" + jsName,
            success: function (data) {
                dialog.dialog('close');
                $("#NewJs").val("");
                handleSuccessMessages(data);
                LoadJs();
                $('.loading').hide();
            },
            error: function (data) {
                handleErrorMessages(data.responseText);
            }
        });
    }
}

function describeMDeveloperControl(developerControlID) {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=LoadDeveloperControlDescription"
      , type: "POST"
      , data: { "ID": developerControlID }
      , success: function (data) {
          $('#describemdevelopercontrol').html(data);
          $('#describemdevelopercontrol').dialog('open');
          $(".loading").hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function deleteFolder(folderPath) {
    $("#pagePath").val(folderPath);
    $('#deletefolder').dialog('open');
}

function deletePage(pagePath) {
    $("#pagePath").val(pagePath);
    $('#deletepage').dialog('open');
}

function deleteFile(filePath) {
    $("#pagePath").val(filePath);
    $('#deletefile').dialog('open');
}

function deleteMDeveloperControl(developerControlID) {
    $("#pagePath").val(developerControlID);
    $('#deletemdevelopercontrol').dialog('open');
}

function deleteRightCol(headerID) {
    $("#pagePath").val(headerID);
    $('#deleterightcol').dialog('open');
}

function deleteLeftNav(headerID) {
    $("#pagePath").val(headerID);
    $('#deleteleftnav').dialog('open');
}

function deleteTopNav(headerID) {
    $("#pagePath").val(headerID);
    $('#deletetopnav').dialog('open');
}

function deleteHeader(headerID) {
    $("#pagePath").val(headerID);
    $('#deleteheader').dialog('open');
}

function deleteFooter(footerID) {
    $("#pagePath").val(footerID);
    $('#deletefooter').dialog('open');
}

function deleteCss(scriptID) {
    $("#pagePath").val(scriptID);
    $('#deletecss').dialog('open');
}

function deleteJs(scriptID) {
    $("#pagePath").val(scriptID);
    $('#deletejs').dialog('open');
}

function renameFolder(folderPath) {
    $("#pagePath").val(folderPath);
    $('#renamefolder').dialog('open');
}

function renamePage(pagePath) {
    $("#pagePath").val(pagePath);
    $('#renamepage').dialog('open');
}

function renameFile(filePath) {
    $("#pagePath").val(filePath);
    $('#renamefile').dialog('open');
}

function renameRightCol(headerID, headerName) {
    $("#pagePath").val(headerID);
    $("#path").val(headerName);
    $('#renamerightcol').dialog('open');
}

function renameLeftNav(headerID, headerName) {
    $("#pagePath").val(headerID);
    $("#path").val(headerName);
    $('#renameleftnav').dialog('open');
}

function renameTopNav(headerID, headerName) {
    $("#pagePath").val(headerID);
    $("#path").val(headerName);
    $('#renametopnav').dialog('open');
}

function renameHeader(headerID, headerName) {
    $("#pagePath").val(headerID);
    $("#path").val(headerName);
    $('#renameheader').dialog('open');
}

function renameFooter(footerID, footerName) {
    $("#pagePath").val(footerID);
    $("#path").val(footerName);
    $('#renamefooter').dialog('open');
}

function renameCss(cssID, cssName) {
    $("#pagePath").val(cssID);
    $("#path").val(cssName);
    $('#renamecss').dialog('open');
}

function renameJs(jsID, jsName) {
    $("#pagePath").val(jsID);
    $("#path").val(jsName);
    $('#renamejs').dialog('open');
}

function permissionsFolder(folderPath) {
    $("#pagePath").val(folderPath);
    $('#permissions').dialog('open');
    $("#itemType").val();
    LoadFolderPermissions(SourceType.Folder);
}

function permissionsPage(pagePath) {
    $("#pagePath").val(pagePath);
    $('#permissions').dialog('open');
    $("#itemType").val(SourceType.Page);
    LoadPagePermissions();
}

function permissionsFile(filePath) {
    $("#pagePath").val(filePath);
    $('#permissions').dialog('open');
    $("#itemType").val(SourceType.File);
    LoadFilePermissions();
}

function LoadFolderPermissions() {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=GetFolderPermissions",
        data: "FolderPath=" + $('#pagePath').val(),
        success: function (data) {
            var oJSON = $.parseJSON(data);
            var strTable = "<table cellspacing=\"0\" class=\"permissions-table\"><thead><tr style=\"background-color:#636363;color:White;\"><th>Allowed User/Role Name</th><th>Action</th></tr></thead><tbody>";
            for (var i = 0; i < oJSON.length; i++) {
                strTable += "<tr style=\"background-color: #" + (i % 2 ? "fff" : "eee") + "\"><td>" + oJSON[i].Name + "</td><td><span style=\"color:Blue;cursor:pointer;text-decoration:underline;\" onclick=\"RemovePermissions('" + oJSON[i].PermissionID + "', 0);\">REMOVE</span></td></tr>";
            }
            strTable += "</tbody></table>";
            $("#permissionsDialogTable").html(strTable);
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function LoadPagePermissions() {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=GetPagePermissions",
        data: "PagePath=" + $('#pagePath').val(),
        success: function (data) {
            var oJSON = $.parseJSON(data);
            var strTable = "<table cellspacing=\"0\" class=\"permissions-table\"><thead><tr style=\"background-color:#636363;color:White;\"><th>Allowed User/Role Name</th><th>Action</th></tr></thead><tbody>";
            for (var i = 0; i < oJSON.length; i++) {
                strTable += "<tr style=\"background-color: #" + (i % 2 ? "fff" : "eee") + "\"><td>" + oJSON[i].Name + "</td><td><span style=\"color:Blue;cursor:pointer;text-decoration:underline;\" onclick=\"RemovePermissions('" + oJSON[i].PermissionID + "', 1);\">REMOVE</span></td></tr>";
            }
            strTable += "</tbody></table>";
            $("#permissionsDialogTable").html(strTable);
            $('.loading').hide();
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function LoadFilePermissions() {
    $(".loading").show();
    $.ajax({
        url: "/lidocaine.ashx?f=GetFilePermissions"
      , type: "POST"
      , data: "VirtualPath=" + $('#pagePath').val()
      , success: function (data) {
          var oJSON = $.parseJSON(data);
          var strTable = "<table cellspacing=\"0\" class=\"permissions-table\"><thead><tr style=\"background-color:#636363;color:White;\"><th>Allowed User/Role Name</th><th>Action</th></tr></thead><tbody>";
          for (var i = 0; i < oJSON.length; i++) {
              strTable += "<tr style=\"background-color: #" + (i % 2 ? "fff" : "eee") + "\"><td>" + oJSON[i].Name + "</td><td><span style=\"color:Blue;cursor:pointer;text-decoration:underline;\" onclick=\"RemovePermissions('" + oJSON[i].PermissionID + "', 2);\">REMOVE</span></td></tr>";
          }
          strTable += "</tbody></table>";
          $("#permissionsDialogTable").html(strTable);
          $('.loading').hide();
      }
      , error: function (data) {
          handleErrorMessages(data.responseText);
      }
    });
}

function AddPermissions(itemPath, allowType, allowID, itemType) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=AddPermissions",
        data: "AllowType=" + allowType + "&AllowID=" + allowID + "&ItemPath=" + itemPath,
        success: function () {
            if (itemType == 0) {
                LoadFolderPermissions();
            }
            else if (itemType == 1) {
                LoadPagePermissions();
            }
            else if (itemType == 2) {
                LoadFilePermissions();
            }
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}

function RemovePermissions(permissionID, itemType) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        url: "/lidocaine.ashx?f=RemovePermissions",
        data: "PermissionID=" + permissionID,
        success: function () {
            if (itemType == 0) {
                LoadFolderPermissions();
            }
            else if (itemType == 1) {
                LoadPagePermissions();
            }
            else if (itemType == 2) {
                LoadFilePermissions();
            }
        },
        error: function (data) {
            handleErrorMessages(data.responseText);
        }
    });
}